% close all
%clear all

data_path='C:\TEMPOUT\';

[data, header, template] = load_tiff_stack(-1,data_path);
data=single(data);
disp('done loading the data!')

nr_frames=size(data,3);

pre_off_frames=2;
post_on_frames=39;

nr_stim_frames=10;
nr_base_frames=25;

bin_stim_ind=zeros(pre_off_frames+post_on_frames+1,1);
bin_stim_ind(pre_off_frames+2:pre_off_frames+2+nr_stim_frames-1)=1;

bin_base_ind=zeros(pre_off_frames+post_on_frames+1,1);
bin_base_ind(end-nr_base_frames+1:end)=1;

stim_ind=[0];
base_ind=[0];
    
for ind=1:ceil(nr_frames/(pre_off_frames+post_on_frames+1))
        stim_ind=[stim_ind bin_stim_ind'];
        base_ind=[base_ind bin_base_ind'];
end

stim_ind=stim_ind(1:nr_frames);
base_ind=base_ind(1:nr_frames);

response_diff=(mean(data(:,:,find(stim_ind)),3)-mean(data(:,:,find(base_ind)),3))./mean(data(:,:,find(base_ind)),3);
response_std=std(data(:,:,find(stim_ind)),0,3);

win_size=4;
response_diff_smoothed = filter2(ones(win_size)/win_size^2,response_diff);

response_diff_smoothed(response_diff_smoothed<prctile(response_diff_smoothed(:),70))=0;


figure;imagesc(response_diff);

figure;
clf; colormap gray
image_data=zeros(size(data,1),size(data,2),3);
image_data(:,:,2)=(mean(data,3)/max(max(mean(data,3))));
image_data(:,:,1)=response_diff_smoothed/max(response_diff_smoothed(:));
image(image_data)
% imagesc(mean(data,3)/max(max(mean(data,3))));
% hold on;
% him=imagesc(response_diff_smoothed/max(response_diff_smoothed(:)));
% set(him,'AlphaData',0.5)


figure;imagesc(response_std);